package com.lishe.lishe.lishe.module.mall.http;

import android.content.Context;
import android.text.TextUtils;

import com.lishe.lishe.asynchttp.HttpUtils;
import com.lishe.lishe.lishe.base.BaseBean;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.mall.bean.MoreItemBean;
import com.lishe.lishe.lishe.module.search.bean.GoodsScreenBean;
import com.lishe.lishe.lishe.utils.LogUtil;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.RetrofitFactory;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shino on 2017/9/19.
 */

public class HttpMall {
    //数据所在的表
    private static final String TABLE_GOODS = "Goods/";
    private static final String TABLE_ORDER = "Order/";

    /**
     * 获取商品详情
     *
     * @param token
     */
    public static void getGoodsDetailInfo(String token, String itemId, ResponseHandlerInterface response) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("itemId", itemId);
        HttpUtils.post(Globals.LISHE_URL + "Home/Goods" + Globals.VERSIONNAME + "/info/itemId/" + itemId, params, response);
    }

    /**
     * 检测库存
     *
     * @param token
     */
    public static void checkSKU(String token, String address, String sku_id, String shop_id, String item_id, ResponseHandlerInterface response) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("address", address);
        params.put("sku_id", sku_id);
        params.put("item_id", item_id);
        params.put("shop_id", shop_id);
        LogUtil.i("商品666", token + "..." + address + "..." + sku_id + "..." + item_id + "..." + shop_id + "...");
        HttpUtils.post(Globals.LISHE_URL + "Home/" + TABLE_ORDER + "checkSku/", params, response);
    }

    /**
     * 添加购物车
     *
     * @param token
     */
    public static void addShoppingCart(String token, String itemId, String skuId, String quantity, String shopId, String jd_ids, String address, ResponseHandlerInterface response) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("itemId", itemId);
        params.put("skuId", skuId);
        params.put("quantity", quantity);
        params.put("shopId", shopId);
        params.put("jd_ids", jd_ids);
        params.put("address", address);
        HttpUtils.post(Globals.LISHE_URL + "Home/" + TABLE_ORDER + "addItemCart/", params, response);
    }

    /**
     * 添加购物车
     *
     * @param token
     */
    public static void getGoodsCountFromShoppingCart(String token, ResponseHandlerInterface response) {
        RequestParams params = new RequestParams();
        params.put("token", token);

        HttpUtils.post(Globals.LISHE_URL + "Home/" + "order/getCartNum/", params, response);
    }

    /**
     * 商城首页
     */
    public static void getMallMain(ResponseHandlerInterface response) {
        RequestParams params = new RequestParams();
        HttpUtils.post(Globals.LISHE_URL + "index1/mallIndex", params, response);
    }

    /**
     * 心意商城首页一级分类接口
     * http://api.lishe.cn/index.php/index1/categoryFirstList
     */
    public static void categoryListFirst(ResponseHandlerInterface response) {

        HttpUtils.post(Globals.LISHE_URL + "index1/categoryFirstList", new RequestParams(), response);
    }

    /**
     * 心意商城首页二级分类接口
     * http://api.lishe.cn/index.php/index1/categorySecondList
     */
    public static void categoryListSecond(Context context, String cate_id, ResponseHandlerInterface response){
        RequestParams params=new RequestParams();
        params.put("cate_id",cate_id);

        HttpUtils.cancelAllRequest();
        HttpUtils.post(context,Globals.LISHE_URL+"index1/categorySecondList" , params, response);
    }

    /**
     * 心意商城首页三级分类接口
     * http://api.lishe.cn/index.php/index1/categoryItems
     *
     * @param cate_id
     * @param original_cate_id
     * @param price            对商品的价格排序((desc（降序）或者asc(升序)）
     * @param sold             对商品的销量排序((desc（降序）或者asc(升序)）
     * @param shop             对商品的来源过滤(数据写法:9,10)
     * @param priceRange       对商品的价格区间过滤(300,1000)
     * @param page
     * @param pageSize
     * @param response
     */
    public static void categoryItems(String cate_id, String original_cate_id, String bind, String price, String sold, String shop, String priceRange, int page, int pageSize, ResponseHandlerInterface response) {
        RequestParams params = new RequestParams();
        params.put("page", page);
        params.put("pageSize", pageSize);
        params.put("cate_id", cate_id);
        params.put("original_cate_id", original_cate_id);
        if (!TextUtils.isEmpty(bind)) {
            params.put("bind", bind);
        }
        if (!TextUtils.isEmpty(price)) {
            params.put("price", price);
        }
        if (!TextUtils.isEmpty(sold)) {
            params.put("sold", sold);
        }
        if (!TextUtils.isEmpty(shop)) {
            params.put("shop", shop);
        }
        if (!TextUtils.isEmpty(priceRange)) {
            params.put("priceRange", priceRange);
        }
        HttpUtils.post(Globals.LISHE_URL+ "index1/categoryItems", params, response);
    }

    /**
     * 猜你喜欢
     */
//    public static void getMaybeLove(String page, String pageSize, String method, HttpResponse.DataResponse response) throws Exception {
//        HashMap<String, String> params = new HashMap<>();
//        params.put("page", page);
//        params.put("pageSize", pageSize);
//        OkHttpUtils.getinstanse().asynPostFormStr(Globals.LISHE_URL + "index1/youLove", params, method, response);
//    }

    /**
     * 猜你喜欢
     */
    public static void getMaybeLove(String page, String pageSize, String method, ResponseHandlerInterface response) throws Exception {
        RequestParams params = new RequestParams();
        params.put("page", page);
        params.put("pageSize", pageSize);
        HttpUtils.post(Globals.LISHE_URL + "index1/youLove", params, response);
    }

    /**
     * 心意商城首页三级分类接口
     * http://api.lishe.cn/index.php/index1/getMoreItems
     *
     * @param activity_config_id 活动页id
     * @param price              对商品的价格排序((desc（降序）或者asc(升序)）
     * @param sold               对商品的销量排序((desc（降序）或者asc(升序)）
     * @param shop               对商品的来源过滤(数据写法:9,10)
     * @param priceRange         对商品的价格区间过滤(300,1000)
     * @param page
     * @param pageSize
     * @param response
     */
    public static void getMoreItem(String activity_config_id, String bind, String price, String sold, String shop, String priceRange, int page, int pageSize, ResponseHandlerInterface response) {
        RequestParams params = new RequestParams();
        params.put("page", page);
        params.put("pageSize", pageSize);
        params.put("activity_config_id", activity_config_id);
        if (!TextUtils.isEmpty(bind)) {
            params.put("bind", bind);
        }
        if (!TextUtils.isEmpty(price)) {
            params.put("price", price);
        }
        if (!TextUtils.isEmpty(price)) {
            params.put("sold", sold);
        }
        if (!TextUtils.isEmpty(price)) {
            params.put("shop", shop);
        }
        if (!TextUtils.isEmpty(price)) {
            params.put("priceRange", priceRange);
        }
        HttpUtils.post(Globals.LISHE_URL + "index1/getMoreItems", params, response);
    }

    /**
     * 获取更多精选商品
     */
    public static void getMoreItems(String item_id, final DataResponseListener<MoreItemBean> listener) {
        RetrofitFactory.create().create(MallNetApi.class).getMoreItems(item_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MoreItemBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {}

                    @Override
                    public void onNext(MoreItemBean bean) {
                        if(bean.getResult()==100 && bean.getErrcode()==0){
                            listener.onSuccess(bean);
                        }else{
                            listener.onFail(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {}
                });
    }

    /**
     * 领取优惠券
     */
    public static void getCoupon(String couponId, String token, final DataResponseListener<BaseBean> listener) {
        RetrofitFactory.create().create(MallNetApi.class).getCoupon(couponId, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {}

                    @Override
                    public void onNext(BaseBean bean) {
                        if(bean.getResult()==100 && bean.getErrcode()==0){
                            listener.onSuccess(bean);
                        }else{
                            listener.onFail(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {}
                });
    }

    /**
     * 获取三级分类商品数据
     */
    public static void getCategoryItems(Map map, final DataResponseListener<GoodsScreenBean> listener) {
        RetrofitFactory.create().create(MallNetApi.class).getCategoryItems(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GoodsScreenBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {}

                    @Override
                    public void onNext(GoodsScreenBean bean) {
                        if(bean.getResult()==100 && bean.getErrcode()==0){
                            listener.onSuccess(bean);
                        }else{
                            listener.onFail(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {}
                });
    }

}
